home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 234_01 / util.c < prev    next >
Text File  |  1987-06-16  |  11KB  |  469 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     XDIR - Hard Disk Manager
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Apr 03, 1987
  6.   DESCRIPTION:     Hard Disk Manager for IBM PC
  7.   KEYWORDS:     Hard Disk Manager Dump Directory
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      util.c
  10.   WARNINGS:     None
  11.   CRC:         N/A
  12.   SEE-ALSO:     HDIR.DOC and XDIR.DOC
  13.   AUTHOR:     Mike Blakley 15645 SW 82 Cir Ln #76, Miami, Fl 33193
  14.   COMPILERS:     ECO-C
  15.   REFERENCES:     XDIR.DOC
  16. */
  17.  
  18.  
  19. /*
  20.    utility routines for xdir
  21.    xrename   - rename a file
  22.    xcopy     - copy files
  23.    xdump     - dump a file  in hex and ascii
  24.    dline     - build a dump line in hex and ascii
  25.    upmod     - change file attributes
  26.    getpath   - get path name
  27. */
  28. #include "stdio.h"
  29. #include "dir.h"
  30. #include "xdir.h"
  31. /*
  32.     xrename
  33.     rename a file
  34.  
  35. */
  36. void xrename(old,new)
  37. char *old, *new;
  38. {
  39.     int  i;
  40.     i = rename(old,new);
  41.     if (i != 0)
  42.        {
  43.        writestr("\nCouldn't rename ");
  44.        writestr(old);
  45.        writestr(" to ");
  46.        writestr(new);
  47.        writestr(" - press enter ");
  48.        getch();
  49.        }
  50.  
  51. }
  52.  
  53. /*
  54.     xcopy
  55.     copy a file
  56.  
  57. */
  58. #define BUFFSIZE 128
  59. void xcopy(fromf,tof)
  60. char *fromf, *tof;
  61. {
  62.      int fdi;
  63.      int fdo;
  64.      static int  i;
  65.      static char buffer[BUFFSIZE];
  66.      static int numbytes;
  67.  
  68.      fdo = creat(tof,0);
  69.      if (fdo == EOF)
  70.         {
  71.         writestr("\nCan't open output file ");
  72.         writestr(tof);
  73.         writestr(" - press any key");
  74.         getch();
  75.         return;
  76.         }
  77.  
  78.      fdi = open(fromf,0);
  79.      if (fdi == EOF)
  80.         {
  81.         writestr("\nCan't open input file ");
  82.         writestr(fromf);
  83.         writestr(" - press any key");
  84.         getch();
  85.         unlink(tof);
  86.         return;
  87.         }
  88.  
  89.       while ((numbytes = read(fdi,buffer,BUFFSIZE)) != EOF)
  90.       {
  91.  
  92.  
  93.       if (numbytes < 0)
  94.          {
  95.          writestr("\nRead error occurred - ");
  96.          writestr(fromf);
  97.          writestr("press any key ");
  98.          getch();
  99.          unlink(tof);
  100.          return;
  101.          }
  102.  
  103.        if (numbytes == 0) break;
  104.        i = write(fdo,buffer,numbytes);
  105.        if (i <= 0)
  106.           {
  107.           writestr("\nError writing output file ");
  108.           writestr(tof);
  109.           writestr(" -press any key");
  110.           getch();
  111.           unlink(tof);
  112.           return;
  113.           }
  114.  
  115.        if (numbytes < BUFFSIZE) break;
  116.        }    /* end while */
  117.  
  118.        i = close(fdi);
  119.        if (i == EOF)
  120.           {
  121.           writestr("\nCouldn't close input ");
  122.           writestr(fromf);
  123.           writestr(" press any key ");
  124.           getch();
  125.           }
  126.  
  127.        i = close(fdo);
  128.        if (i == EOF)
  129.           {
  130.           writestr("\nCouldn't close output ");
  131.           writestr(tof);
  132.           writestr(" press any key ");
  133.           getch();
  134.           }
  135.  
  136. }        /* end function */
  137.  
  138. /*
  139.     xdump
  140.     dump a file
  141.  
  142. */
  143. #define DUMPSIZE 256
  144. void xdump(fnam)
  145. char *fnam;
  146. {
  147.      int    fdi;                   /* file descriptor */
  148.      static int  i,j, k, c;
  149.      static char buffer[DUMPSIZE];
  150.      static int numbytes;
  151.      char   pline[79];
  152.      long   offset;                /* byte offset of record displayed */
  153.      static int currpage, lastpage;
  154.      long   foff, lseek();
  155.      char   temp[20];
  156.  
  157.      currpage = 0;
  158.      offset = 0L;
  159.      fdi = open(fnam,0);
  160.      if (fdi == EOF)
  161.         {
  162.         writestr("\nCan't open input file ");
  163.         writestr(fnam);
  164.         writestr(" - press any key");
  165.         getch();
  166.         return;
  167.         }
  168.       foff =  lseek(fdi,0L,2);                /* search end of file */
  169.       foff /= DUMPSIZE;
  170.       lastpage = (int) foff;                  /* determine last page */
  171.  
  172.       while (1)
  173.       {
  174.       foff = (long) currpage;                /* current page */
  175.       foff *= (long) DUMPSIZE;
  176.       offset = lseek(fdi,foff,0);              /* do a seek to position */
  177.  
  178.  
  179.       numbytes = read(fdi,buffer,DUMPSIZE);
  180.  
  181.  
  182.       if (numbytes < 0)
  183.          {
  184.          writestr("\nRead error occurred - ");
  185.          writestr(fnam);
  186.          writestr("press any key ");
  187.          getch();
  188.          return;
  189.          }
  190.  
  191.  
  192.        k = DUMPSIZE - numbytes;     /* indicate eof fill pattern */
  193.        for (j=0;j<k;j++) buffer[numbytes+j] = 0x1a;
  194.  
  195.        clrscr();
  196.        writestr("\n\033[1mDump of file \033[0m");
  197.        writestr("\033[7m");
  198.        writestr(fnam);
  199.        writestr("\033[0m");
  200.        writestr("\n\n\n\n");
  201.  
  202.        for (i=0;i<16;i++)
  203.        {
  204.        j = i * 16;
  205.        dline(buffer+j,pline,offset+(long)j);   /* build dump line */
  206.        putchar('\n');
  207.        writestr(pline);
  208.        }
  209.  
  210.        writestr("\n\n\n\033[7mEnd of page -\033[0m");
  211.        writestr("\033[1mESC\033[0m = Exit PgUp PgDn Home End +n -n F1=Help ");
  212.  
  213.        c = toupper(getch());
  214.        if (c == 0) c= getch();     /* pickup up special keys */
  215.  
  216.        if (c == '\033') break;
  217.        else
  218.        if (c == 73)    /* PgUp */ --currpage;
  219.        else
  220.        if (c == 81)    /* PgDn */ ++currpage;
  221.        else
  222.        if (c == 13)    /* CR   */ ++currpage;
  223.        else
  224.        if (c == 79)    /* End  */ currpage = lastpage;
  225.        else
  226.        if (c == 71)    /* Home */ currpage = 0;
  227.        else
  228.        if (c == '+')
  229.           {
  230.           putchar(c);
  231.           gets(temp);
  232.           currpage += atoi(temp);
  233.           }
  234.        else
  235.        if (c == '-')
  236.           {
  237.           putchar(c);
  238.           gets(temp);
  239.           currpage -= atoi(temp);
  240.           }
  241.        else
  242.        if (c == 59)     /* Help */
  243.           {
  244.           clrscr();
  245.           writestr(" Hex/Ascii Dump - Help Screen ");
  246.           writestr("\n\n\nKey                Function");
  247.           writestr("\n_____________________________\n");
  248.           writestr("\nPgUp               Previous Page");
  249.           writestr("\nPgDn               Next Page    ");
  250.           writestr("\nHome               Restart at beginning ");
  251.           writestr("\nEnd                Go to End    ");
  252.           writestr("\nF1                 Help - This screen ");
  253.           writestr("\n+nnn               Forward  nnn 256 byte blocks ");
  254.           writestr("\n-nnn               Backward nnn 256 byte blocks ");
  255.           writestr("\n<CR>               Next Page ");
  256.           writestr("\n\n\nPress any key to continue ");
  257.           getch();
  258.           }
  259.  
  260.        if (currpage < 1) currpage = 0;
  261.        if (currpage > lastpage) currpage = lastpage;
  262.  
  263.        }    /* end while */
  264.  
  265.        i = close(fdi);
  266.        if (i == EOF)
  267.           {
  268.           writestr("\nCouldn't close input ");
  269.           writestr(fnam);
  270.           writestr(" press any key ");
  271.           getch();
  272.           }
  273.  
  274. }        /* end function */
  275.  
  276.  
  277. /*
  278.    dline
  279.    build a dump line from 16 bytes
  280.  
  281. */
  282. void dline(ibuff,xbuff,offset)
  283. char *ibuff, *xbuff;
  284. long  offset;
  285. {
  286.      int  i,j,k,c;
  287.      char lbuff[80], rbuff[20], obuff[10];
  288.      char *cpl, *cpr;
  289.  
  290.  
  291.      clear(lbuff,80,' ');
  292.      clear(rbuff,20,' ');
  293.      cpl = lbuff;
  294.      cpr = rbuff;
  295.  
  296.      j = k = (int) offset;
  297.      j &= 0xf000;
  298.      j >>= 12;
  299.      j &= 0x0f;
  300.      if (j < 10) obuff[0] = j + '0';
  301.         else     obuff[0] = j + 'a' - 10;
  302.      j = k & 0x0f00;
  303.      j >>= 8;
  304.      if (j < 10) obuff[1] = j + '0';
  305.         else     obuff[1] = j + 'a' - 10;
  306.      j = k & 0x00f0;
  307.      j >>= 4;
  308.      if (j < 10) obuff[2] = j + '0';
  309.         else     obuff[2] = j + 'a' - 10;
  310.      j = k & 0x000f;
  311.      if (j < 10) obuff[3] = j + '0';
  312.         else     obuff[3] = j + 'a' - 10;
  313.      obuff[4] = 0;
  314.  
  315.      for (i=0;i<16;i++)
  316.      {
  317.      c = (int) *ibuff++;   /* get character */
  318.      if ((c >= 0x20) && (c <= 0x7f))
  319.         *cpr++ = c;
  320.         else *cpr++ = '.';
  321.  
  322.      j = (c & 0xf0);
  323.      j >>= 4;
  324.      if (j < 10) *cpl++ = j + '0';
  325.         else *cpl++ = j + 'a' - 10;
  326.  
  327.      j = (c & 0x0f);
  328.      if (j < 10) *cpl++ = j + '0';
  329.         else *cpl++ = j + 'a' - 10;
  330.      *cpl++ = ' ';
  331.      }
  332.      *cpl = *cpr = 0;
  333.  
  334.      strcpy(xbuff,obuff);
  335.      strcat(xbuff,"  ");
  336.      strcat(xbuff,lbuff);
  337.      strcat(xbuff,"    ");
  338.